<?php
namespace app\http\middleware;
use think\facade\Request;
use think\Db;
class VerifyLogin
{
    public function handle($request, \Closure $next)
    {
        $userInfo = [];
        $head_params = Request::header('','');
        if($head_params){
            foreach ($head_params as $k=>$v){
                $head_params[strtolower($k)] = $v;
            }
        }
        $params = Request::param('','','strip_tags,trim');
        if($params){
            foreach ($params as $k=>$v){
                $params[strtolower($k)] = $v;
            }
        }

        $version = '';
        $code = '';
        $token = '';
        $scale_grade = 'C';
        $quick_app_id = 0;

        if(isset($head_params['version']) && strlen($head_params['version']) > 0){
            $version = $head_params['version'];
        }
        if(isset($head_params['code']) && strlen($head_params['code']) > 0){
            $code = $head_params['code'];
        }
        if(isset($head_params['authorization']) && strlen($head_params['authorization']) > 1){
            $token = $head_params['authorization'];
        }
        if(strlen($code) < 1)
        {
            return json(['code'=>101,'msg'=>'渠道标识不能为空','data'=>[]]);
        }

        if(strlen($token) < 1){
            return json(['code'=>101,'msg'=>'登录凭证不能为空','data'=>[]]);
        }

        $agentRecord = Db::table('cms_manager_user')->where('channel_code',$code)->field('id,parent_id,scale')->findOrEmpty();
        if(empty($agentRecord)){
            return json(['code'=>101,'msg'=>'渠道标识无效','data'=>[]]);
        }

        $check = Db::table('quick_user')->where('token',$token)
            ->field('id,quick_id,agent_id,channel_id,provider,regdate,regtime,pay_times')->findOrEmpty();

        if (empty($check)) {
            return json(['code' => 103, 'msg' => '登录凭证已失效']);
        }

        $userInfo = $check;
        $blackRecord = Db::table('quick_list_black')->where('userId',$userInfo['id'])
                        ->field('id,status')->findOrEmpty();

        if($blackRecord && $blackRecord['status'] >= 1)
        {
            return json(['code'=>103,'msg'=>'用户状态异常','data'=>[]]);
        }

        if(!$userInfo){
            return json(['code'=>103,'msg'=>'无效token,请检查！','data'=>[]]);
        }

        if( strlen($agentRecord['scale']) >= 1 )
        {
            $scale_grade = $agentRecord['scale'];
        }

        $quickInfo = Db::table('quick_app_mp_manager')->where('uid',$agentRecord['id'])->field('quick_id')->findOrEmpty();
        if($quickInfo)
        {
            $quick_app_id = $quickInfo['quick_id'];
        }


        $request->userInfo      = $userInfo;
        $request->agentId       = $agentRecord['parent_id'];
        $request->channelId     = $agentRecord['id'];
        $request->channelCode   = $code;
        $request->version       = $version;
        $request->scale_grade   = $scale_grade;
        $request->quickAppId    = $quick_app_id;

        return $next($request);
    }
}
